package com.ljh.user.config;

import com.ljh.common.constants.Constants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: Themberfue
 * @date: 2025/4/25 12:03
 * @description:
 */
@Configuration
public class RabbitConfig {
    @Bean("helloQueue")
    public Queue queue() {
        return QueueBuilder.durable("hello").build();
    }

    /**
     * 队列，起名为：user.queue
     * @return Queue
     */
    @Bean("userQueue")
    public Queue userQueue() {
        return QueueBuilder
                .durable(Constants.USER_QUEUE_NAME)
                .build();
    }

    /**
     * 交换机，起名为：user.exchange
     * @return FanoutExchange
     */
    @Bean("userExchange")
    public FanoutExchange userExchange() {
        return ExchangeBuilder
                .fanoutExchange(Constants.USER_EXCHANGE_NAME)
                .durable(true)
                .build();
    }

    /**
     * 将队列和交换机绑定
     * @param userQueue 队列
     * @param userExchange 交换机
     * @return Binding
     */
    @Bean("userBinding")
    public Binding binding(@Qualifier("userQueue") Queue userQueue,
                           @Qualifier("userExchange") FanoutExchange userExchange) {
        return BindingBuilder
                .bind(userQueue)
                .to(userExchange);
    }
}
